//
//  BangFamilyUserVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/22.
//  Copyright © 2020 tym. All rights reserved.
//

#import "BangFamilyUserVC.h"
#import "Sizes.h"
#import "LockUsersVC.h"
#import "FamilyService.h"
#import "AddRoomCell.h"
#import "UIButton+Gradient.h"
#import "LYEmptyViewHeader.h"
#import "EquipmentService.h"
#import "GroupShadowTableView.h"
@interface BangFamilyUserVC ()<GroupShadowTableViewDelegate,GroupShadowTableViewDataSource>

@property (nonatomic, strong) GroupShadowTableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,weak)  UIButton *saveBtn;
@property(nonatomic,strong) EquipmentService  *equipService;

@property(nonatomic,strong) FamilyService *familyService;


@property(nonatomic,assign) NSInteger selePath;
@end

@implementation BangFamilyUserVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"绑定家庭成员"];
    self.selePath= -1;
    
    [self setTable];
    
    TYMButton *button = [TYMButton btn_text:@"保存" color:[UIColor whiteColor] font:Font_Medium(17)];
    button.frame = CGRectMake(30, ScreenHeight - NavigateonHeight - 74-SafeBottomMargin, ScreenWidth - 60, 44);
    [button gradientButtonWithSize:button.frame.size colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    [button addTarget:self action:@selector(saveSeleUserAction:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    [self.view addSubview:button];
    
    self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img"
                                                             titleStr:@"这个门锁还没有家庭用户哦"
                                                            detailStr:@""];
    [self getDataArrayWithWeb];
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight - TabbarHeight*2.5) style:UITableViewStyleGrouped];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.groupShadowDelegate = self;
    _tableView.groupShadowDataSource = self;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)]; view.backgroundColor = [UIColor redColor]; self.tableView.tableHeaderView = view;
    
    [self.view addSubview:_tableView];
    self.saveBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
    self.saveBtn.layer.cornerRadius = 5;
    self.saveBtn.layer.borderWidth = 1;
    self.saveBtn.layer.masksToBounds = YES;
}

-(EquipmentService*)equipService{
    if (_equipService == nil) {
        _equipService = [[EquipmentService alloc] init];
    }
    return _equipService;
}

-(FamilyService*)familyService{
    if (_familyService == nil) {
        _familyService = [[FamilyService alloc] init];
    }
    return _familyService;
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(void)getDataArrayWithWeb{
    TYMWeak(self);
    [self.equipService getEquipmentuseruserall:self.equipmentID familyId:[UserTool getCurrentFamily].familyId completion:^(LMJBaseResponse * _Nonnull respons, NSArray<UserModel *> * _Nonnull users) {
        if(weakself.dataArray.count){
            [weakself.dataArray removeAllObjects];
        }
        for (UserModel *user in users) {
            if (self.type) {
                if (user.type == 1) {
                    [weakself.dataArray addObject:user];
                }
            }else{
                [weakself.dataArray addObject:user];
            }
        }
        if (weakself.dataArray.count) {
            weakself.tableView.headTitles = @[@"选择家庭成员"];
            weakself.tableView.isShowArray = @[@(1)];
            [weakself.tableView reloadData];
        }else{
            [weakself.tableView ly_showEmptyView];
        }
    }];
}

//保存
-(void)saveSeleUserAction:(id)sender{
    
    if(!self.dataArray.count || self.selePath==-1){
        return;
    }
    
    UserModel *userModel  = self.dataArray[self.selePath];
    TYMWeak(self);
    if(self.type == 0){
        [self.equipService getEquipmentuserbindappuserEquipuserId:self.equipmentUserID userId:userModel.userId type:userModel.type completion:^(LMJBaseResponse * _Nonnull respons,EquipmentUserModel *userModel) {
            if (respons.message) {
                [WHToast showMessage:respons.message duration:2 finishHandler:nil];
            }else{
                [weakself.navigationController popViewControllerAnimated:YES];
            }
        }];
    }else{
        [self.familyService getGroupuserbindappuserFamily:[UserTool getCurrentFamily].familyId groupUserId:self.youkeModel.userId userId:userModel.userId equipmentId:self.equipmentID Completion:^(LMJBaseResponse * _Nonnull responsed) {
            if (responsed.message) {
                [WHToast showMessage:responsed.message duration:2 finishHandler:nil];
            }else{
                [WHToast showMessage:@"绑定成功" duration:2 finishHandler:^{
                    for (UIViewController *controller in weakself.navigationController.viewControllers) {
                        if ([controller isKindOfClass:[LockUsersVC class]]) {
                            [weakself.navigationController popToViewController:controller animated:YES];
                        }
                    }
                }];
            }
        }];
    }
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [AddRoomCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddRoomCell *cell = [tableView dequeueReusableCellWithIdentifier:[AddRoomCell identifier]];
    if (cell==nil) {
        cell = [[AddRoomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[AddRoomCell identifier]];
    }
    UserModel *model = self.dataArray[indexPath.row];
    cell.userModel = model;
    
    if (self.selePath == indexPath.row) {
        cell.righImage.image = [UIImage imageNamed:@"icon_home12"];
    }else{
        cell.righImage.image = [UIImage imageNamed:@"icon_home13"];
    }
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.row == self.selePath){
        self.selePath = -1;
    }else{
        self.selePath= indexPath.row;
    }
    [self.tableView reloadData];
}


@end
